package com.slmxz.tms.document.controller;

import com.slmxz.tms.common.model.Query;
import com.slmxz.tms.common.result.PageResult;
import com.slmxz.tms.common.result.Result;
import com.slmxz.tms.common.support.Condition;
import com.slmxz.tms.document.model.entity.Contractor;
import com.slmxz.tms.document.model.vo.ContractorVO;
import com.slmxz.tms.document.service.ContractorService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import jakarta.validation.Valid;

/**
 * 承运商信息前端控制层
 * @author sunyao
 * @Date 2024-11-15
 */
@Tag(name = "承运商信息接口")
@RestController
@RequestMapping("/document/contractor")
@RequiredArgsConstructor
public class ContractorController  {

    private final ContractorService contractorService;

    @Operation(summary = "承运商信息分页列表")
    @GetMapping("/page")
    public PageResult<ContractorVO> selectContractorPage(Query query, ContractorVO contractor) {
        IPage<ContractorVO> result = this.contractorService
                .selectContractorPage(Condition.getPage(query),contractor);
        return PageResult.success(result);
    }

    @Operation(summary = "承运商信息的下拉框")
    @GetMapping("/selectOption")
    public Result<?> selectOption(ContractorVO contractor){
        return Result.success(this.contractorService.selectOption(contractor));
    }

    @Operation(summary = "新增承运商信息")
    @PostMapping("/save")
    @PreAuthorize("@ss.hasPerm('document:contractor:add')")
    public Result<Void> saveContractor(@RequestBody @Valid ContractorVO  contractor) {
        boolean result = this.contractorService.saveContractor(contractor);
        return Result.result(result);
    }

    @Operation(summary = "获取承运商信息详情数据")
    @GetMapping("/detail/{id}")
    @PreAuthorize("@ss.hasPerm('document:contractor:edit')")
    public Result<Contractor> selectContractorDetail(
        @Parameter(description = "承运商信息ID") @PathVariable Long id
    ) {
        Contractor contractor = this.contractorService.selectContractorDetail(id);
        return Result.success(contractor);
    }

    @Operation(summary = "修改承运商信息")
    @PutMapping(value = "/update")
    @PreAuthorize("@ss.hasPerm('document:contractor:edit')")
    public Result<Void> updateContractor(
            @RequestBody @Validated Contractor contractor
    ) {
        boolean result = contractorService.updateContractor(contractor);
        return Result.result(result);
    }

    @Operation(summary = "删除承运商信息")
    @DeleteMapping("/remove/{ids}")
    @PreAuthorize("@ss.hasPerm('document:contractor:delete')")
    public Result<Void> deleteContractors(
        @Parameter(description = "承运商信息ID，多个以英文逗号(,)分割") @PathVariable String ids
    ) {
        boolean result = this.contractorService.deleteContractors(ids);
        return Result.result(result);
    }

    @Operation(summary = "激活或者注销承运商信息")
    @PostMapping("/activeOrLogout")
    @PreAuthorize("@ss.hasPerm('document:business:delete')")
    public Result<Void> activeOrLogout(@RequestBody ContractorVO contractor) {
        boolean result = this.contractorService.activeOrLogout(contractor);
        return Result.result(result);
    }
}
